package com.jumeng.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumeng.entity.CourseEntity;
import com.jumeng.entity.OrderEntity;
import com.jumeng.enums.OrderStatus;
import com.jumeng.mapper.OrderMapper;
import com.jumeng.server.OrderService;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public OrderEntity selectByOrderNo(String orderNo) {
        LambdaQueryWrapper<OrderEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(OrderEntity::getOrderNo, orderNo);
        return orderMapper.selectOne(wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateOrderStatus(Map<String, String> notifyMap) {
        // 更新订单状态
        String orderNo = notifyMap.get("out_trade_no");
        OrderEntity orderEntity = this.selectByOrderNo(orderNo);
        orderEntity.setStatus(OrderStatus.PRICE_SUCCESS);//支付成功
        baseMapper.updateById(orderEntity);
    }
}
